package cn.lyh.cn.lyh;
import cn.lyh.cn.lyh.handler.CustomerBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/test1")
    public String test1(){
        return "--------------------test1";
    }
    @GetMapping("/test2")
    public String test2(){
        return "--------------------test2";
    }


    @GetMapping("/testD")
    public String testD(){
        try {TimeUnit.SECONDS.sleep(1);  } catch (InterruptedException e) {e.printStackTrace();}
        log.info(">>>>>>>>>>testD测试");
        return "--------------------testD";
    }

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handlerByResource")//不加blockHandler前端会errorPage
    public String byResource(){
        log.info(">>>>>>>>>>teRequestParam试");
        return "--------------RequestParam--testD";
    }

    public String handlerByResource(BlockException exception){
        return "handlerTestHotKey/(ㄒoㄒ)/~~";
    }
    @GetMapping("/customerBlock")
    @SentinelResource(value = "customerBlock",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException")
    public String customerBlock(){
        log.info(">>>>>>>>>>teRequestParam试");
        return "--------------RequestParam--testD";
    }

}
